/**
 * Project Name:mauto-web
 * File Name:ApiUser.java
 * Package Name:com.inftech.mauto.jy.web.jsPad
 * Date:2017年6月16日下午12:52:59
 * Copyright (c) 2017, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.jy.web.jsPad;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inftech.mauto.common.base.BaseController;
import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.common.utils.SendSmsUtil;
import com.inftech.mauto.jy.entity.Technician;
import com.inftech.mauto.jy.service.JyEnterpriseService;
import com.inftech.mauto.jy.service.TechnicianService;
import com.inftech.mauto.jy.web.CompanyAction;
import com.mingsoft.util.StringUtil;

/**
 * ClassName:ApiUser <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Date: 2017年6月16日 下午12:52:59 <br/>
 * 
 * @author Administrator
 * @version
 * @since JDK 1.7
 */

@Controller
@RequestMapping(value = "/api/js/account", produces = "application/json;charset=utf-8")
@Transactional(rollbackFor=Exception.class)
public class ApiJsAccountController extends BaseController {
	Log log = LogFactory.getLog(CompanyAction.class);
	/**
	 * 企业Service
	 */
	@Resource(name = "jyEnterpriseBizImpl")
	private JyEnterpriseService jyEnterpriseBizImpl;
	/**
	 * 技师Service
	 */
	@Resource
	private TechnicianService technicianBizImpl;

	/**
	 * 
	 * login:(手机号登录). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param phone
	 * @param pwd
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/login")
	@ResponseBody
	public String login(HttpServletRequest request, HttpServletResponse response) {
		String outJson = "";
		try {
			String phone = request.getParameter("phone");
			String pwd = request.getParameter("pwd");
			Technician technican = technicianBizImpl.getTechnicianByPhone(phone);
			Map<String, Object> map = new HashMap<String, Object>();

			String token = null;
			if (technican == null) {
				map.put("token", token);
				HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_702;
				outJson = getOutJson1(statusCode, false, statusCode.toString(),map );
				log.info("无该帐号！");
			} else {
				if (!technican.getPwd().equals(StringUtil.Md5(pwd))) {
					map.put("token", token);
					HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_701;
					outJson = getOutJson1(statusCode, false, statusCode.toString(), map);
					log.info("密码不正确！");
				} else {
					HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
					token = StringUtil.Md5(technican.getPhone() + technican.getPwd());
					technicianBizImpl.updateByPhone(phone, token);
					map.put("token", token);
					outJson = getOutJson1(statusCode, true, statusCode.toString(),map );
					log.info("获取登录信息成功！");
				}
			}
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			log.error("获取登录信息失败！", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * sendVcode:(发送验证码). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/sendVcode")
	@ResponseBody
	public String sendVcode(HttpServletRequest request, HttpServletResponse response) {
		String phone = request.getParameter("phone");
		Map<String, Object> map = new HashMap<String, Object>();
		int code = CommonUtils.getRandomInt();
		String msg = "你的验证码是:" + code + "，欢迎使用米拓车服";
		String outJson = "";
		try {
			SendSmsUtil.batchSend(phone, msg);
		} catch (Exception e) {
			map.put("vcode", null);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_703;
			outJson = getOutJson1(statusCode, false, statusCode.toString(), map);
			log.error("发送验证码失败", e);
			return outJson;
		}
		map.put("vcode", code);
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		outJson = getOutJson1(statusCode, true, statusCode.toString(), map);
		log.info("发送验证码成功！");
		return outJson;
	}

	/**
	 * 
	 * savePwdByPhone:(保存新密码). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/savePwdByPhone")
	@ResponseBody
	public String savePwdByPhone(HttpServletRequest request, HttpServletResponse response) {
		String outJson = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			String phone = request.getParameter("phone");
			String pwd = request.getParameter("pwd");
			pwd = StringUtil.Md5(pwd);
			map.put("phone", phone);
			map.put("pwd", pwd);
			String token = StringUtil.Md5(phone + pwd);
			map.put("token", token);
			int row = technicianBizImpl.updatePwdAndTokenByphone(map);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
			log.info("根据手机号码修改密码成功！");
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			log.error("根据手机号码修改密码失败！", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * savePwdByOld:(旧密码修改密码). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/savePwdByOld")
	@ResponseBody
	public String savePwdByOld(HttpServletRequest request, HttpServletResponse response) {
		String outJson = null;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			String oldToken = request.getParameter("token");
			String oldpwd = request.getParameter("oldpwd");
			String newpwd = request.getParameter("newpwd");
			map.put("oldToken", oldToken);
			map.put("oldpwd", oldpwd);
			map.put("newpwd", newpwd);
			String phone = technicianBizImpl.getPhoneByToken(oldToken);
			String newToken = StringUtil.Md5(phone + newpwd);
			map.put("newToken", newToken);
			int row = technicianBizImpl.updatePwdAndTokenByToken(map);
			if (row == 1) {
				HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
				outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
				log.info("修改成功!");
			} else {
				HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_705;
				outJson = getOutJson1(statusCode, false, statusCode.toString(), null);
				log.info("旧密码不正确!");
			}
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			log.error("修改失败！", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson1(statusCode, true, statusCode.toString(), null);
		}
		return outJson;
	}

}
